# -*- coding: utf-8 -*-
import traceback
from utils.backcode import ImportError
from app.manage.recourseManage.common.manage_sql import ResourceManageSql
from utils.logger import Md_Debug
from utils.db_utils import DbConn
import datetime


class RecourseManageDB(object):
    """视频"""
    def __init__(self):
        self.db = DbConn()
        self.now = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d")
        self.time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")

    def recourse_list(self, body):
        """任务列表"""
        try:
            ret = dict()
            db = DbConn()
            page = body.get('page', None)
            size = body.get('size', None)

            # check parameter
            if not page:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "page is none"
            elif not size:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "size is none"
            else:
                list_data = db.fetch_all(ResourceManageSql.RECOURSE_LIST)
                page_list = RecourseManageDB.paging(self, list_data, page, size)
                code = ImportError.IMPORT_CODE_SUCCESS
                ret["response_data"] = page_list
                message = "success"
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret

    def edit_recourse(self, body):
        """编辑、新增人力信息"""
        try:
            ret = dict()
            applicant_name = body.get("applicant_name")
            station = body.get("station")
            tel = body.get("applicant_name")
            preliminary_result = body.get("applicant_name")
            preliminary_opinion = body.get("station")
            reexamination_result = body.get("applicant_name")
            reexamination_opinion = body.get("station")
            final_result = body.get("applicant_name")
            final_opinion = body.get("station")
            appointment = body.get("applicant_name")
            # 0:新增 1：修改
            type = body.get("type")

            # check parameter
            if not applicant_name:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "applicant_name is none"
            elif not station:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "station is none"
            elif not tel:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "tel is none"
            elif not preliminary_result:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "preliminary_result is none"
            elif not preliminary_opinion:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "preliminary_opinion is none"
            elif not reexamination_result:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "reexamination_result is none"
            elif not reexamination_opinion:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "reexamination_opinion is none"
            elif not final_result:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "final_result is none"
            elif not final_opinion:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "final_opinion is none"
            elif not appointment:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "appointment is none"
            elif not type:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "type is none"
            else:
                if type == 0:
                    params = [applicant_name, station, tel, preliminary_result, preliminary_opinion, reexamination_result,
                              reexamination_opinion, final_result, final_opinion, appointment]
                    self.db.fetch_one(ResourceManageSql.ADD_RECOURSE, params)
                elif type == 1:
                    recourse_id = body.get("recourse_id")
                    if not recourse_id:
                        code = ImportError.IMPORT_CODE_INVALID_ARGS
                        message = "recourse_id is none"
                    params = [applicant_name, station, tel, preliminary_result, preliminary_opinion, reexamination_result,
                              reexamination_opinion, final_result, final_opinion, appointment, recourse_id]
                    self.db.commit(ResourceManageSql.EDIT_RECOURSE, params)
                else:
                    pass
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret

    def paging(self, dataList, page, size):
        """分页"""
        ret = {
               'page': page, 'size': size, 'total_page': 0, 'total_size': 0
               }
        total_size = len(dataList)
        if total_size % size == 0:
            total_page = total_size / size
        else:
            total_page = total_size / size + 1
        start_index = (page - 1) * size
        end_index = page * size
        ret['total_page'] = total_page
        ret['total_size'] = total_size
        ret['data'] = dataList[start_index: end_index]
        return ret

